package chuangyi.com.org.ViewModel;

import android.content.Context;
import android.databinding.Bindable;
import android.support.annotation.LayoutRes;
import android.view.View;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.NotifyParentCheckedChanged;

/**
 * Created by zxh on 16/12/6.
 */

public class CheckedMenuItem extends MenuItem {
    private boolean checked;
    private Object parent;

    public Object getParent() {
        return parent;
    }

    public void setParent(Object parent) {
        this.parent = parent;
    }

    @Bindable
    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        if (this.checked != checked) {
            this.checked = checked;
            if (parent instanceof NotifyParentCheckedChanged) {
                NotifyParentCheckedChanged notify = (NotifyParentCheckedChanged) parent;
                notify.notifyCheckedChanged(this);
            }
        }
        notifyPropertyChanged(BR.checked);
    }

    void setCheckedInner(boolean checked) {
        this.checked = checked;
        notifyPropertyChanged(BR.checked);
    }

    public CheckedMenuItem(Context context, @LayoutRes int layoutId) {
        super(context, layoutId);
    }

    public CheckedMenuItem(Context context, @LayoutRes int layoutId, String title, String code) {
        super(context, layoutId);
        super.setTitle(title);
        super.setCode(code);
    }

    /**
     * 排序或专家筛选弹窗的点击逻辑，请勿使用
     *
     * @param view
     */
    public void onCheckMenu(View view) {
        if (parent instanceof NotifyParentCheckedChanged) {
            NotifyParentCheckedChanged notify = (NotifyParentCheckedChanged) parent;
            if (this.checked == false) {
                this.setChecked(true);
            }
            notify.notifyCheckedChanged(this);
        } else {
            this.setChecked(!checked);
        }
    }
}
